﻿using System;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using ET;
using BLL;

namespace ERP.PageUC
{
    public partial class WareHouseUC : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridDrop();
            }
        }

        protected void btnSavePopup_Click(object sender, EventArgs e)
        {
            if (lbID.Value == "")
            {
                InsertWarehouse();
            }
            else
            {
                UpdateWarehouse();
            }
        }

        protected void btnReloadPopup_Click(object sender, EventArgs e)
        {
            BindGridDrop();
        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            var clsWarehouse = new Warehouse_BLL();
            var dtWarehouse = clsWarehouse.GetWarehouse();
            if (dtWarehouse != null && dtWarehouse.Rows.Count > 0)
            {
                foreach (DataRow dr in dtWarehouse.Rows)
                {
                    if (txtWarehouse_Alias.Text == dr["Warehouse_Alias"].ToString())
                    {
                        kiemtra = true;
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Trùng mã kho !";
                        OpenDialog("#thongbao");
                        if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertWarehouse()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new Warehouse_ET
                    {
                        Warehouse_Alias = txtWarehouse_Alias.Text,
                        Warehouse_Name = txtWarehouse_Name.Text,
                        Branch_Id = int.Parse(drBranch.SelectedValue),
                        Warehouse_Note = txtGhiChu.Text
                    };
                    Warehouse_BLL.InsertWarehouse(obj);
                    ClearTextBox();
                    if (Page.Master != null)
                        ((Label)Page.Master.FindControl("lbThongBao")).Text = "Thêm mới thành công !"; 
                }
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtWarehouse_Alias.Enabled = true;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        protected void UpdateWarehouse()
        {
            try
            {
                var obj = new Warehouse_ET
                {
                    Warehouse_Id = int.Parse(lbID.Value),
                    Warehouse_Alias = txtWarehouse_Alias.Text,
                    Warehouse_Name = txtWarehouse_Name.Text,
                    Branch_Id = int.Parse(drBranch.SelectedValue),
                    Warehouse_Note = txtGhiChu.Text
                };
                Warehouse_BLL.UpdateWarehouse(obj);
                ((Page.Warehouse)Page).GetWarhouse();
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Sữa thành công !";
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtWarehouse_Alias.Enabled = false;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        protected void BindGridDrop()
        {
            var clsChiNhanh = new Branch_BLL();
            var dtChiNhanh = clsChiNhanh.GetBranch();
            if (dtChiNhanh != null && dtChiNhanh.Rows.Count > 0)
            {
                drBranch.Items.Clear();
                drBranch.Items.Add(new ListItem("-Vui lòng chọn chi nhánh-", "-1"));
                drBranch.DataSource = dtChiNhanh;
                drBranch.DataValueField = "STT";
                drBranch.DataTextField = "TENCHINHANH";
                drBranch.DataBind();
            }
        }
        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }
        #endregion
        #region[ClearTextbox]
        protected void ClearTextBox()
        {
            txtWarehouse_Alias.Text = string.Empty;
            txtWarehouse_Name.Text = string.Empty;
            drBranch.SelectedIndex = 0;
            txtGhiChu.Text = string.Empty;
        }
        #endregion
    }
}